import ssl

# 全局取消证书验证 根据⾃⼰环境进⾏使⽤
# sl._create_default_https_context = ssl._create_unverified_context

from qcloudsms_py import SmsMultiSender, SmsSingleSender
from qcloudsms_py.httpclient import HTTPError

import random

# 应⽤ID
TENCENT_SMS_APP_ID = "1400839807"
# 应⽤KEY
TENCENT_SMS_APP_KEY = "28deb66a1a9a2cf2cfff1dd9e4ec3e63"
# ⾃⼰填写的签名内容
TENCENT_SMS_SIGN = "小王的仓库公众号"
# ⼿机号
phone_num = 13971315021
# 模版id
template_id = 1868403
code = random.randrange(1000, 9999)
# 模版参数
template_param_list = [code]


def send_sms_single(phone_num, template_id, template_param_list):
    """
     单条发送短信
     :param phone_num: ⼿机号
     :param template_id: 腾讯云短信模板ID
     :param template_param_list: 短信模板所需参数列表，
     例如:【验证码：{1}，描述：{2}】，
     则传递参数 [888,666]按顺序去格式化模板
     :return:
     """
    appid = TENCENT_SMS_APP_ID
    appkey = TENCENT_SMS_APP_KEY
    sms_sign = TENCENT_SMS_SIGN
    sender = SmsSingleSender(appid, appkey)
    try:
        response = sender.send_with_param(86, phone_num, template_id, template_param_list, sign=sms_sign)
    except HTTPError as e:
        response = {'result': 1000, 'errmsg': "发送失败"}
    return response


response = send_sms_single(phone_num, template_id, template_param_list)
